package com.lei.zjs.web.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 流程实例Action
 * 
 * @author Spinacia
 * 
 */
@Controller
@Scope("prototype")
public class ProcessInstanceAction extends ActionSupport {

	@Resource
	private ProcessEngine processEngine;

	// 查询所有流程实例
	public String list() {
		ProcessInstanceQuery processInstanceQuery = processEngine
				.getRuntimeService().createProcessInstanceQuery();
		List<ProcessInstance> list = processInstanceQuery.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}

	private String id;

	public void setId(String id) {
		this.id = id;
	}

	// 根据id查询变量
	public String findData() throws IOException {
		Map<String, Object> map = processEngine.getRuntimeService()
				.getVariables(id);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(map);
		return NONE;
	}

	// 查看流程图
	public String showPng() {
		ProcessInstanceQuery instanceQuery = processEngine.getRuntimeService()
				.createProcessInstanceQuery().processInstanceId(id);
		ProcessInstance processInstance = instanceQuery.singleResult();
		String processDefinitionId = processInstance.getProcessDefinitionId();
		ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) processEngine
				.getRepositoryService().getProcessDefinition(
						processDefinitionId);
		//部署id
		String deploymentId = processDefinition.getDeploymentId();
		String pngName = processDefinition.getDiagramResourceName();
		
		String activityId = processInstance.getActivityId();
		ActivityImpl activityImpl = processDefinition.findActivity(activityId);
		
		int x = activityImpl.getX() - 5;
		int y = activityImpl.getY() - 5;
		int width = activityImpl.getWidth() + 10;
		int height = activityImpl.getHeight() + 10;
		
		ActionContext.getContext().getValueStack().set("deploymentId", deploymentId);
		ActionContext.getContext().getValueStack().set("imageName", pngName);
		ActionContext.getContext().getValueStack().set("x", x);
		ActionContext.getContext().getValueStack().set("y", y);
		ActionContext.getContext().getValueStack().set("width", width);
		ActionContext.getContext().getValueStack().set("height", height);
		
		return "image";
	}
	
	private String deploymentId;
	private String imageName;
	private InputStream pngStream;
	
	public InputStream getPngStream() {
		return pngStream;
	}

	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}

	public void setImageName(String imageName) {
		this.imageName = imageName;
	}

	//显示流程图
	public String viewImage(){
		
		pngStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId, imageName);
		return "viewpng";
	}
}
